<template>
  <div class="totalback">
      <h3 class="titles">{{titles}}</h3>
    <el-form
      :model="data"
      status-icon
      :rules="rules"
      ref="data"
      label-width="100px"
      class="demo-ruleForm total"
    >
      <el-form-item label="名称" prop="name">
        <el-input type="text" v-model="data.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input type="text" v-model="data.address" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="telephone">
        <el-input v-model.number="data.telephone"></el-input>
      </el-form-item>
      <el-form-item label="门店描述" prop="desc">
        <el-input type="textarea" rows="6" v-model="data.desc" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('data')">提交</el-button>
        <el-button @click="resetForm('data')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props:["titles","data"],
  data() {
    let phonere = /^1(3|4|5|6|7|8|9)\d{9}$/;
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("电话不能为空"));
      } else if (!phonere.test(value)) {
        return callback(new Error("请输入电话号"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入名称"));
      } else {
        if (this.data.address !== "") {
          this.$refs.data.validateField("address");
        }
        callback();
      }
    };
    var desc = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请对您的店铺进行描述"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入地址"));
      } else {
        callback();
      }
    };
    return {
      shopData: {
        name: "", // 名称
        address: "", // 地址
        telephone: "", // 电话
        desc: "" // 门店描述
      },
      rules: {
        name: [{ validator: validatePass, trigger: "blur" }],
        address: [{ validator: validatePass2, trigger: "blur" }],
        telephone: [{ validator: checkAge, trigger: "blur" }],
        desc: [{ validator: desc, trigger: "blur" }]
      }
    };
  },
  mounted(){
    this.data=this.data;
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("操作成功!");
         this.$emit("handle",this.data)
        } else {
          alert("请完善信息 !!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style>
</style>